package util;

import java.util.*;
import model.*;

public class GenericComparator<T> implements Comparator<T> {

	private String _fielName = "Id";
	private boolean _desc = false;

	public GenericComparator(String fielName, boolean desc) {
		_fielName = fielName;
		desc = _desc;
	}

	@Override
	public int compare(T o1, T o2) {
		int res = 0;
		if (o1 instanceof QuotationItemModel) {
			res = compareQuotationItem(o1, o2);
		} else if (o1 instanceof PhaseModel) {
			res = comparePhase(o1, o2);
		}

		if (_desc) {
			res = -res;
		}

		return res;
	}

	public int compareQuotationItem(T o1, T o2) {
		QuotationItemModel obj1 = (QuotationItemModel) o1;
		QuotationItemModel obj2 = (QuotationItemModel) o2;

		int res = 0;

		if (_fielName.equalsIgnoreCase(QuotationItemModel.FieldNames.DisplayIndex)) {
			res = obj1.getDisplayIndex().compareToIgnoreCase(obj2.getDisplayIndex());
		}

		return res;
	}

	public int comparePhase(T o1, T o2) {
		PhaseModel obj1 = (PhaseModel) o1;
		PhaseModel obj2 = (PhaseModel) o2;

		int res = 0;

		if (_fielName.equalsIgnoreCase(PhaseModel.FieldNames.Index)) {
			if (obj1.getIndex() < obj2.getIndex()) {
				res = -1;
			} else if (obj1.getIndex() > obj2.getIndex()) {
				res = 1;
			}
		}

		return res;
	}

}
